package com.cqvip.innocence.project.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqvip.innocence.project.mapper.AdminMapper;
import com.cqvip.innocence.project.model.dto.AdminRoleView;
import com.cqvip.innocence.project.model.entity.Admin;
import com.cqvip.innocence.project.service.AdminService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName AdminServiceImplements
 * @Description
 * @Author Innocence
 * @Date 2020/7/11
 * @Version 1.0
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public Page<AdminRoleView> getAdminsList(Page page, AdminRoleView adminRoleView) {
        Page<AdminRoleView> list = baseMapper.getAdminsList(page,adminRoleView);
        List<AdminRoleView> records = list.getRecords();
        Set<String> set = new HashSet<>();
        List<AdminRoleView> newList = new ArrayList<>();
        for (AdminRoleView arv:records) {
            if (!set.contains(arv.getAdminName())){
                set.add(arv.getAdminName());
                newList.add(arv);
            }
        }
        for (int i = 0; i < newList.size(); i++) {
            List<Long> ids = new ArrayList<>();
            List<String> names = new ArrayList<>();
            Long roleId = newList.get(i).getRoleId();
            String roleName = newList.get(i).getRoleName();
            if (roleId == null){continue;}
            ids.add(roleId);
            names.add(roleName);
            for (int j = 0; j <records.size(); j++) {
                Long id = records.get(j).getRoleId();
                if (id==null){continue;}
                if (newList.get(i).getAdminName().equals(records.get(j).getAdminName()) && !roleId
                        .equals(id)){
                    ids.add(records.get(j).getRoleId());
                    names.add(records.get(j).getRoleName());
                }
            }
            newList.get(i).setRoleIds(ids);
            newList.get(i).setRoleNames(names);
        }
        list.setRecords(newList);
        list.setTotal(newList.size());
        return list;
    }

}
